import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;

public class StableMarriage {
    private String[] menNames;
    private String[] womenNames;
    private int[][] menPrefs;
    private int[][] womenPrefs;
    private LinkedList<Integer> freeMen;
    private int[] engagedWomen;
    private int[] engagedMen;

    public static void main(String[] args) throws IOException {
        if (args.length > 0) {
            StableMarriage sm = new StableMarriage();
            sm.readInput(new File(args[0]));
            long startTime = System.currentTimeMillis();
            sm.solve();
            long endTime = System.currentTimeMillis();
            BufferedWriter writer = new BufferedWriter(new FileWriter("output.out"));
            for (int i = 1; i < sm.engagedWomen.length; i++) {
                writer.write(sm.menNames[i] + " -- " + sm.womenNames[sm.engagedMen[i]]);
                writer.newLine();
            }
            writer.flush();
            writer.close();
            System.out.printf("\nAlgorithm took %.4f seconds (excluding parsing and printing out).", (endTime - startTime) / 1000.0);
        }
    }

    private void readInput(File file) {
        try {
            Scanner sc = new Scanner(file);
            String line = sc.nextLine();

            while (line.startsWith("#"))
                line = sc.nextLine();

            int n = Integer.parseInt(line.substring(2));

            menNames = new String[n + 1];
            womenNames = new String[n + 1];
            freeMen = new LinkedList<Integer>();
            for (int i = 1; i <= n * 2; i++) {
                sc.skip("[0-9]+ "); // Skip numbers.
                if (i % 2 == 1) {
                    menNames[(i + 1) / 2] = sc.nextLine();
                    freeMen.add((i + 1) / 2);
                } else {
                    womenNames[i / 2] = sc.nextLine();
                }
            }
            sc.nextLine();
            menPrefs = new int[n + 1][n + 1];
            womenPrefs = new int[n + 1][n + 1];
            engagedWomen = new int[n + 1];
            engagedMen = new int[n + 1];
            for (int i = 1; i <= n * 2; i++) {
                sc.skip("[0-9]+: "); // Skip numbers.
                if (i % 2 == 1) {
                    for (int j = 1; j <= n; j++)
                        menPrefs[(i + 1) / 2][j] = sc.nextInt() / 2;
                } else {
                    for (int j = 1; j <= n; j++)
                        womenPrefs[i / 2][(sc.nextInt() + 1) / 2] = j;
                }
                sc.nextLine();
            }
            sc.close();
        } catch (FileNotFoundException e) {
            System.out.println("No such file.");
        }
    }

    private void solve() {
        int[] next = new int[menNames.length];
        while (!freeMen.isEmpty()) {
            int currentMan = freeMen.getFirst();
            next[currentMan]++;
            int currentWoman = menPrefs[currentMan][next[currentMan]];

            if (engagedWomen[currentWoman] == 0) {
                engagedWomen[currentWoman] = currentMan;
                engagedMen[currentMan] = currentWoman;
                freeMen.removeFirst();
            } else {
                if (womenPrefs[currentWoman][currentMan] < womenPrefs[currentWoman][engagedWomen[currentWoman]]) {
                    freeMen.removeFirst();
                    freeMen.add(0, engagedWomen[currentWoman]);
                    engagedWomen[currentWoman] = currentMan;
                    engagedMen[currentMan] = currentWoman;
                }
            }
        }
    }
}